﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace QuanLyKhachSan
{
    public partial class frmQuanLyDatPhong : DevExpress.XtraEditors.XtraForm
    {
        DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);

        public frmQuanLyDatPhong()
        {
            InitializeComponent();
        }

        private void btDatPhong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmDatPhong frmdatphong = new frmDatPhong();
            frmdatphong.frmquanlydatphong = this;
            frmdatphong.ShowDialog();
        }

        private void frmQuanLyDatPhong_Load(object sender, EventArgs e)
        {
            this.fillQuanLyDatPhong();
        }
       public   void fillQuanLyDatPhong()
        {
            DataClassesKhachSanDataContext db1 = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);
            var datphong = db1.DanhSachDatPhong();
            gcDatPhong.DataSource = datphong;
        }
        private void gvDatPhong_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "")
            {
                if (e.RowHandle >= 0)
                {
                    e.DisplayText = (e.RowHandle + 1).ToString();
                }
            }
        }

        private void btXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult result = XtraMessageBox.Show("Bạn có chắc muốn xóa?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (result == DialogResult.Yes)
            {
                try
                {
                    int chitietid = int.Parse(gvDatPhong.GetFocusedRowCellValue("CTDP_ID").ToString());
                    var chitiet = (from d in db.CHI_TIET_DAT_PHONGs
                                  where d.CTDP_ID == chitietid
                                  select d).Single ();
                    db.CHI_TIET_DAT_PHONGs.DeleteOnSubmit(chitiet);

                    

                    //cap nhat trang thai cua phong
                    var phong = db.PHONGs.Single(p => p.P_ID == chitiet.P_ID);
                    if (phong .TT_ID != 5)
                    {
                        phong.TT_ID = 1;
                    }
                    db.SubmitChanges();
                    XtraMessageBox.Show("Đã xóa thành công đặt phòng");
                    this.fillQuanLyDatPhong();
                }
                catch (Exception)
                {
                    XtraMessageBox.Show("Xóa thất bại, vui lòng kiểm tra lại.");
                }

            }

        }
    }
}